package com.emmy.walnut.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.emmy.walnut.R;
import com.emmy.walnut.adapter.ClassAdapter;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.Label;
import com.emmy.walnut.presenter.iface.ClassPIface;
import com.emmy.walnut.presenter.ClassPresenter;
import com.emmy.walnut.ui.EditTextDialog;
import com.emmy.walnut.util.SoftKeyboardUtil;
import com.emmy.walnut.view.iface.ClassIface;

import java.util.ArrayList;
import java.util.List;


public class ClassFragment extends Fragment implements ClassIface {

    private FloatingActionButton mAddLabel;

    private RecyclerView classRecyclerView;
    private ClassAdapter classAdapter;

    private ClassPIface classPIface;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_class, container, false);
        initSystemparams();
        initView(view);
        return view;
    }

    private void initSystemparams(){
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    private void initView(final View view) {

        classPIface = new ClassPresenter(this);

        classRecyclerView = view.findViewById(R.id.classes);
        classRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));

        classAdapter = new ClassAdapter(getContext(), AppConstant.activities);

        classRecyclerView.setAdapter(classAdapter);

        mAddLabel = view.findViewById(R.id.addClass);

        mAddLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditTextDialog editTextDialog = new EditTextDialog(getContext());
                editTextDialog.setOnClickListener(new EditTextDialog.OnClickListener() {
                    @Override
                    public void onCancelListener() {
                        editTextDialog.dismiss();
                    }

                    @Override
                    public void onConfirmListener() {
                        List<View> views = new ArrayList<>();
                        views.add(editTextDialog.getEditText());
                        SoftKeyboardUtil.hideSoftKeyboard(getContext(), views);
                        CharSequence text = editTextDialog.getEditText().getText();
                        if (text!=null&&text.length()>0){
                            if (text.length()>8){
                                Toast.makeText(getContext(), "请保持标签名的简洁", Toast.LENGTH_SHORT).show();
                            }else{
                                classPIface.addLabel(text.toString());
                            }
                            editTextDialog.dismiss();
                        }else{
                            Toast.makeText(getContext(), "请输入标签名", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                editTextDialog.show();
            }
        });
    }

    @Override
    public void addLabelDuplicate() {
        Toast.makeText(getContext(), "当前标签名已存在", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void addLabelSuccess(Label label) {
        AppConstant.activities.add(label);
        classAdapter.notifyDataSetChanged();
    }
}
